package io.renren.modules.sp.controller.front;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.QccUtil;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sp.dto.SpTeamDTO;
import io.renren.modules.sp.dto.SpUserDTO;
import io.renren.modules.sp.entity.SpTeamEntity;
import io.renren.modules.sp.service.SpTeamService;
import io.renren.modules.sp.service.SpTeamUserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;


/**
* 领队
*
* @author Mark sunlightcs@gmail.com
* @since 3.0 2023-04-20
*/
@RestController
@RequestMapping("front/spteam")
@Api(tags="领队")
public class FrontTeamController extends FrontBaseController{

    @Autowired
    private SpTeamService spTeamService;
    @Autowired
    private SpTeamUserService spTeamUserService;

    @GetMapping("{id}")
    public Result<Map<String,Object>> get(@PathVariable("id") Long id, HttpServletRequest request){
        SpUserDTO user = this.getFrontUserInfo(request);
        if(user == null){
            return new Result<Map<String,Object>>().error("请先登录");
        }
        return spTeamUserService.queryTeamUserList(user);
    }

    /**
     * 创建领队
     * @param dto
     * @return
     */
    @PostMapping("save")
    public Result save(@RequestBody SpTeamDTO dto, HttpServletRequest request){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        SpUserDTO user = this.getFrontUserInfo(request);
        if(user == null){
            return new Result<Map<String,Object>>().error("请先登录");
        }

        // 参赛代表队名称为固定值时，不调用企查查接口
        if(!"!0@9#8$7%6".equals(dto.getTeamName())){
            // 校验组织机构代码证
            boolean isCompany = QccUtil.isCompany(dto.getCompany(), dto.getCompanyNo());
            if(!isCompany){
                return new Result<SpUserDTO>().error("单位信息不存在");
            }
        }else{
            // 参赛代表队名称为固定值时，单位名称作为参赛代表队名称
            dto.setTeamName(dto.getCompany());
        }

        /**
         * 代表队的成员：
         * 领队-1人、副领队-最多3人、
         * 指导老师、选手
         * 验证逻辑：
         * 1、一个领队只能带一个代表队，一个代表队只能一个领队
         */
        dto.setCreator(user.getId());
        String teamName = dto.getTeamName();
        if(StringUtils.isEmpty(teamName)){
            dto.setTeamName(dto.getCompany());
        }
        return spTeamService.saveTeam(dto);
    }

    @PostMapping("update")
    public Result update(@RequestBody SpTeamDTO dto, HttpServletRequest request){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        return spTeamService.updateTeam(dto);
    }

    @PostMapping("reAuthCompany")
    public Result reAuthCompany(@RequestBody SpTeamDTO dto, HttpServletRequest request){
        //校验每年只能更新一次
        SpTeamEntity spTeamEntity = spTeamService.selectById(dto.getId());
        String year = DateUtils.getYear();
        if(year.equals(spTeamEntity.getUpdComYear())){
            return new Result<Map<String,Object>>().error("该单位信息已更新过，无需再次更新");
        }
        //单位名称是否已经在系统存在
        List<SpTeamEntity> list = spTeamService.isExistCompany(dto.getCompany());
        if(list!=null && list.size() > 0){
            return new Result<Map<String,Object>>().error("该单位已有代表队");
        }
        String companyNo = spTeamEntity.getCompanyNo();
        if(!companyNo.equals(dto.getCompanyNo())){
            return new Result<Map<String,Object>>().error("统一社会信用码与原代表队信息不一致");
        }
        // 校验组织机构代码证
        boolean isCompany = QccUtil.isCompany(dto.getCompany(), dto.getCompanyNo());
        if(!isCompany){
            return new Result<SpUserDTO>().error("单位信息不存在");
        }
        //更新单位名称
        SpTeamEntity entity = new SpTeamEntity();
        entity.setId(dto.getId());
        entity.setCompany(dto.getCompany());
        entity.setCompanyNo(dto.getCompanyNo());
        entity.setUpdComTimes(1);
        //根据系统时间更新年份
        entity.setUpdComYear(DateUtils.getYear());
        spTeamService.updateById(entity);
        return new Result();
    }




}
